<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎注册图书商城</view>
			<input class="u-border-bottom" v-model="username" placeholder="请输入昵称" />
			<input class="u-border-bottom" v-model="email" placeholder="请输入邮箱" />
			<input class="u-border-bottom" type="password" v-model="password1" placeholder="请输入密码" />
			<input class="u-border-bottom" type="password" v-model="password2" placeholder="请再一次输入密码" />
			<button @tap="submit" :style="[inputStyle]" class="getCaptcha">注册</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: '泰坦',
				email: '147258369@a.com',
				password1: '123123',
				password2: '123123'
			}
		},
		
		computed:{
			inputStyle() {
				let style = {}
				if(this.$u.test.email(this.email)) {
					style.color = "#fff"
					style.backgroundColor = this.$u.color['warning']
				}
				return style
			}
		},
		methods: {
			
			submit(){
				if(!this.$u.test.email(this.email) || !this.password1 || !this.password2 || !this.username) return
				if(this.password1 != this.password2) {
					
					this.$u.toast('两次密码不一致请检查')
					return false
				}
				
				this.getRegister()
				this.$u.toast('注册成功，正在为您跳转')
				const backUrl = uni.getStorageSync('back_url') || '/pages/index/index'
				setTimeout(()=>{
					this.$u.route({
						type: 'reLaunch',
						url: backUrl
					})
					
				},1500)
			},
			async getRegister(){
				const paramsRegister = {
					name: this.username,
					email: this.email,
					password: this.password1,
					password_confirmation: this.password2
				}
			    const res = await this.$u.api.register(paramsRegister)
				console.log(res);
			}
		}
	}
</script>

<style lang="scss" scoped>

	.wrap {
		font-size: 28rpx;
		.content {
			width: 600rpx;
			margin: 80rpx auto 0;
			}
		.title {
			text-align: left;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 100rpx;
		}
		.u-border-bottom{
		margin-top: 30rpx !important;
		}
		.getCaptcha {
			background-color: rgb(253, 243, 208);
			color: $u-tips-color;
			border: none;
			font-size: 30rpx;
			padding: 12rpx 0;
			margin: 40rpx 0rpx;
			&::after {
				border: none;
			}
		}
	}
</style>
